#include "delay.h"
void delay(volatile unsigned int d)
{
	while(d--);
}

void delay_init(void)
{
	GPT1->CR = 0; 					
	GPT1->CR = 1 << 15;				
	while((GPT1->CR >> 15) & 0x01);	
	GPT1->CR = (1<<6);
	GPT1->PR = 65;	
	GPT1->OCR[0] = 0XFFFFFFFF;
	GPT1->CR |= 1<<0;			
	
}


void delayus(unsigned int usdelay)
{
	unsigned long oldcnt,newcnt;
	unsigned long tcntvalue = 0;	/* 走过的总时间  */

	oldcnt = GPT1->CNT;
	while(1)
	{
		newcnt = GPT1->CNT;
		if(newcnt != oldcnt)
		{
			if(newcnt > oldcnt)		/* GPT是向上计数器,并且没有溢出 */
				tcntvalue += newcnt - oldcnt;
			else  					/* 发生溢出    */
				tcntvalue += 0XFFFFFFFF-oldcnt + newcnt;
			oldcnt = newcnt;
			if(tcntvalue >= usdelay)/* 延时时间到了 */
			break;			 		/*  跳出 */
		}
	}
}


void delayms(unsigned int msdelay)
{
	int i = 0;
	for(i=0; i<msdelay; i++)
	{
		delayus(1000);
	}
}
